<template>
  <view class="table-body">
    <!-- 父级行 -->
    <view class="parent-row" @click="toggleExpand()">
      <view class="col name">{{ item.goodsTitle }}</view>
      <view class="col">{{ item.inventoryQuantity }}</view>
      <view class="col">{{ item.totalInventoryQuantity }}</view>
      <view class="col action">
        <uni-icons :type="expanded!=null ? (item.expanded ? 'arrowup' : 'arrowdown'):''" size="16"
                   color="#666"/>
      </view>
    </view>

    <!-- 子级行（展开时显示） -->
    <view v-if="expanded" class="child-rows">
      <view v-for="child in childList" :key="child.id" class="child-row">
        <view class="child-row-click" @click="openChildExpand(child)">
          <view class="col name">{{ child.specsTitle }}</view>
          <view class="col">{{ child.inventoryQuantity }}</view>
          <view class="col">{{ child.totalInventoryQuantity }}</view>
          <view class="col action">
            <uni-icons :type="child.expanded ? 'arrowup' : 'arrowdown'" size="16"
                       color="#666"/>
          </view>
        </view>

        <!-- 子级行（展开时显示） -->
        <view class="grandchild-rows" v-if="child.expanded">
          <view v-for="child1 in dowList" :key="child1.id" class="grandchild-row">
            <view class="grandchild-row-click" @click="handleChildAction(child1)">
              <view class="col name">{{ child1.shopName }}</view>
              <view class="col"></view>
              <view class="col">{{ child1.quantity }}</view>
            </view>
          </view>
          <view class="grandchild-rows" v-if="dowList.length==0">
            <uni-load-more :status="loadingStatus"/>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>
<script>
import UniIcons from "../../../uni_modules/uni-icons/components/uni-icons/uni-icons.vue";
import _ from "lodash";
import SMS from "../../functional/popup/SMS.vue";
import {getAction} from "../../../utils/action";
import UniLoadMore from "../../../uni_modules/uni-load-more/components/uni-load-more/uni-load-more.vue";

export default {
  name: "inventoryTreeListView",
  components: {UniLoadMore, SMS, UniIcons},
  props: {
    item: Object,
  },
  data() {
    return {
      childList: [], // 第二级
      dowList: [], //最底级
      expanded: false,
      childExpanded: false,
      loadingStatus: 'more',
      url: {
        shopInventoryList: "api/boss/shopInventoryList"
      }
    }
  },
  watch: {
    item: {
      deep: true, // 深度监听对象变化
      immediate: true, // 首次立即执行
      handler(newVal) {
        if (newVal.childrenList) {
          // 执行数据相关操作（如更新本地状态）
          this.$nextTick(() => {
            this.childList = newVal.childrenList.map(item => ({...item, childExpanded: false}));
          })
        }
      }
    }
  },
  mounted() {
    this.expanded = this.item.expanded
  },
  methods: {
    // 切换展开状态
    toggleExpand() {
      if (this.item.goodsTitle !== "合计")
        this.$set(this, 'expanded', !this.expanded); // 只需这一行
    },
    //子项展开状态
    async openChildExpand(child) {
      this.loadingStatus = "loading";
      this.childList = this.childList.map(item => {
        if (item.specsId === child.specsId) {
          return {...item, expanded: !item.expanded}
        }
        return {...item, expanded: false} // 确保单层展开
      })
      let res = await getAction(this.url.shopInventoryList, {specsId: child.specsId})
      if (res.success) {
        this.dowList = res.result
      }
      this.$nextTick(() => {
        this.loadingStatus = 'noMore';
      })
    },
    // 最底项按钮操作
    handleChildAction(child) {
      this.$emit('open-sms', child.goodsSms);
    },
  }
}
</script>
<style scoped lang="scss">
.parent-row {
  background-color: #fff;
  display: grid;
  grid-template-columns: 2fr 1fr 1fr 0.8fr;
  align-items: center;
  padding: 25rpx 0;
  border-bottom: 1rpx solid #eee;
}

.child-row {
  border-bottom: 1rpx solid #eee;

  &::before {
    content: "";
    position: absolute;
    left: 20rpx;
    top: 50%;
    width: 15rpx;
    height: 1px;
  }

  .child-row-click {
    height: 75rpx;
    grid-template-columns: 2fr 1fr 1fr 0.8fr;
    display: grid;
    align-items: center;
    position: relative;
  }
}

.grandchild-row {
  padding-left: 80rpx; // 三级缩进
  border-top: 1rpx solid #eee;

  .grandchild-row-click {
    height: 75rpx;
    grid-template-columns: 2fr 1fr 1fr 0.8fr;
    display: grid;
    align-items: center;
    position: relative;
  }
}

.col {
  flex: 1;
  text-align: center;
  font-size: 22rpx;
  color: #333;
}

.name {
  text-align: left !important;
  padding-left: 20px;
}

.child-row .name {
  padding-left: 80rpx;
}

.action {
  display: flex;
  justify-content: center;
}

</style>
